package cn.wolfcode.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.enums.BusinessReportEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.BusinessReportQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {
    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;
    @RequestMapping("/list")
    @RequiredPermission(name="报表显示",expression ="businessReport:list")
    public String list(@ModelAttribute("qo") BusinessReportQueryObject qo, Model model){
        PageInfo pageInfo = businessReportService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        //传入所有门店
        model.addAttribute("businesses",businessService.listAll());
        //传入所有的结算状态
        model.addAttribute("status", ConsumptionStatusEnum.values());
        //传入所有的分组
        model.addAttribute("groupIds", BusinessReportEnum.values());
        return "/businessReport/list";
    }

    @RequestMapping("/listBar")
    @RequiredPermission(name="报表显示",expression ="businessReport:listBar")
    public String listBar(@ModelAttribute("qo") BusinessReportQueryObject qo, Model model){
        //存放分组类别
        ArrayList<Object> groupTypeList = new ArrayList<>();
        ArrayList<Object> totalCountList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        //查询所有的数据
        qo.setPageSize(0);
        List<HashMap> list = businessReportService.query(qo).getList();
        //将数据放到容器中
        list.forEach(m->{
            groupTypeList.add(m.get("groupType"));
            totalCountList.add(m.get("totalCount"));
            totalAmountList.add(m.get("totalAmount"));
            discountAmountList.add(m.get("discountAmount"));
            payAmountList.add(m.get("payAmount"));
        });
        //将字符集合中的元素转化成带引号的形式
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("totalCountList", JSON.toJSONString(totalCountList));
        model.addAttribute("totalAmountList", JSON.toJSONString(totalAmountList));
        model.addAttribute("discountAmountList", JSON.toJSONString(discountAmountList));
        model.addAttribute("payAmountList", JSON.toJSONString(payAmountList));
        return "/businessReport/listBar";
    }
}
